﻿#include "foadddialog.h"
#include "ui_foadddialog.h"
#include <QSqlQuery>
FOaddDialog::FOaddDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FOaddDialog)
{
    ui->setupUi(this);
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("添加信息状态：");
    ui->dtOutTime->setDate(QDate::currentDate());
    connect(ui->btnOk,SIGNAL(clicked(bool)),parent,SLOT(FOshow()));
}

FOaddDialog::~FOaddDialog()
{delete ui;}

void FOaddDialog::on_btnOk_clicked()
{
    strOutTime =  ui->dtOutTime->date().toString("yyyy-MM-dd");//转出时间
    strName = ui->edtName->text();
    if(ui->cbBxIDType->currentIndex() != 7){
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+1);
    }else{
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+2);
    }
    strID = ui->edtID->text();
    strPreDuty = ui->edtPreDuty->text();
    strToUnit = ui->edtOutToUnit->text();
    strOutReason = ui->txtOutReason->toPlainText();
    strDuringPeople = ui->edtDuringPeople->text();
    strRemark = ui->txtDescription->toPlainText();

    if(strName.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入姓名！");
    }else if(strName.size() > 50){
        ui->status->setText("添加信息状态：姓名信息输入有误！");
    }else if(strID.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入证件号！");
    }else if(strID.size() > 18){
        ui->status->setText("添加信息状态：证件号信息输入有误！");
    }else if(strPreDuty.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入原单位职务！");
    }else if(strPreDuty.size() > 80){
        ui->status->setText("添加信息状态：原单位职务信息输入有误！");
    }else if(strToUnit.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入转去单位！");
    }else if(strToUnit.size() > 120){
        ui->status->setText("添加信息状态：转去单位信息输入有误！");
    }else if(strOutReason.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入转出原因！");
    }else if(strOutReason.size() > 500){
        ui->status->setText("添加信息状态：转出原因信息输入有误！");
    }else if(strDuringPeople.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入经办人！");
    }else if(strDuringPeople.size() > 50){
        ui->status->setText("添加信息状态：经办人信息输入有误！");
    }else{
        QString strInsert=QString("insert into FileOut values (null,'%1','%2','%3','%4','%5','%6','%7','%8','%9'); ")
                .arg(strOutTime).arg(strName).arg(strIDType).arg(strID).arg(strPreDuty)
                .arg(strToUnit).arg(strOutReason).arg(strDuringPeople).arg(strRemark);
        QSqlQuery query;
        if(query.exec(strInsert)){
            ui->status->setText("添加信息状态：添加成功！");
            ui->edtName->clear();
            ui->edtID->clear();
            ui->edtPreDuty->clear();
            ui->edtOutToUnit->clear();
            ui->txtOutReason->clear();
            ui->edtDuringPeople->clear();
            ui->txtDescription->clear();
        }else{
            ui->status->setText("添加信息状态：添加失败！");
        }
    }
}

void FOaddDialog::on_btnCancel_clicked()
{
    this->close();
}
